﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Dcms.Common;

/// <summary>
/// 通用分页控件
/// 创建者：钟顺平
/// 创建时间：2005-6-12
/// </summary>
public partial class pagecontrol : System.Web.UI.UserControl
{
    public string LinkUrl;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string TotalRecord = Utils.GetCookie("TotalRecord");
            double PageSize = Convert.ToDouble(Utils.GetCookie("PageSize"));
            /*
            if (Utils.StrToInt(TotalRecord, 0) == 0)
            {
                TotalRecord = "100";
            }
            if (Utils.StrToInt(PageSize, 0) == 0)
            {
                PageSize = 10;
            }
            */
            int CurPage = DcmsRequest.GetQueryInt("Page", 1);
            LinkUrl = DcmsRequest.GetPageName();
            if (DcmsRequest.GetQueryInt("CateId", 0) > 0)
            {
                LinkUrl = LinkUrl + "?CateId=" + DcmsRequest.GetQueryInt("CateId", 0) + "&Page=";
            }
            else
            {
                string KeyWord = Utils.ChkSQL(Request["KeyWord"]);
                if (KeyWord.Length > 0)
                {
                    LinkUrl = LinkUrl + "?KeyWord=" + KeyWord + "&Page=";
                }
                else
                {
                    LinkUrl = LinkUrl + "?Page=";
                }
            }
            //计算总页数开始
            double TotalPage = Convert.ToDouble(TotalRecord) / PageSize;
            int EndPage = Convert.ToInt32(Math.Ceiling(TotalPage));
            //计算结束

            //分页处理开始
            string PageListText = string.Empty;
            this.First.Text = LinkUrl + "1";
            this.Pre.Text = LinkUrl + (CurPage - 1);
            this.Last.Text = LinkUrl + EndPage;
            this.Next.Text = LinkUrl + (CurPage + 1);
            CurPage = (CurPage > EndPage) ? EndPage : CurPage;
            if (CurPage <= 1 && EndPage > 1)
            {
                this.First.Text = "#";
                this.Pre.Text = "#";
            }
            else if (CurPage == EndPage && CurPage == 1)
            {
                this.First.Text = "#";
                this.Pre.Text = "#";
                this.Last.Text = "#";
                this.Next.Text = "#";
            }
            else if (CurPage == EndPage && CurPage != 1)
            {
                this.Last.Text = "#";
                this.Next.Text = "#";
            }

            if ((CurPage + 5 <= EndPage) && ((CurPage - 5) >= 1))
            {
                for (int i = CurPage - 5; i <= (CurPage + 5); i++)
                {
                    string pNum = string.Empty;
                    if (i < 10)
                    {
                        pNum = "0" + i.ToString();
                    }
                    else
                    {
                        pNum = i.ToString();
                    }
                    if (i != CurPage)
                    {
                        PageListText = PageListText + "<a href=\"" + LinkUrl + i + "\" class=\"p_num\">" + pNum + "</a>\n";
                    }
                    else
                    {
                        PageListText = PageListText + "<a class=\"p_curpage\">" + pNum + "</a>\n";
                    }
                }
            }
            else if (CurPage <= 5 && EndPage >= 11)
            {
                for (int i = 1; i <= 11; i++)
                {
                    string pNum = string.Empty;
                    if (i < 10)
                    {
                        pNum = "0" + i.ToString();
                    }
                    else
                    {
                        pNum = i.ToString();
                    }
                    if (i != CurPage)
                    {
                        PageListText = PageListText + "<a href=\"" + LinkUrl + i + "\" class=\"p_num\">" + pNum + "</a>\n";
                    }
                    else
                    {
                        PageListText = PageListText + "<a class=\"p_curpage\">" + pNum + "</a>\n";
                    }
                }
            }
            else if (CurPage + 5 >= EndPage && CurPage - 5 >= 1)
            {
                for (int i = CurPage - 5; i <= EndPage; i++)
                {
                    string pNum = string.Empty;
                    if (i < 10)
                    {
                        pNum = "0" + i.ToString();
                    }
                    else
                    {
                        pNum = i.ToString();
                    }
                    if (i != CurPage)
                    {
                        PageListText = PageListText + "<a href=\"" + LinkUrl + i + "\" class=\"p_num\">" + pNum + "</a>\n";
                    }
                    else
                    {
                        PageListText = PageListText + "<a class=\"p_curpage\">" + pNum + "</a>\n";
                    }
                }
            }
            else
            {
                for (int i = 1; i <= EndPage; i++)
                {
                    string pNum = string.Empty;
                    if (i < 10)
                    {
                        pNum = "0" + i.ToString();
                    }
                    else
                    {
                        pNum = i.ToString();
                    }
                    if (i != CurPage)
                    {
                        PageListText = PageListText + "<a href=\"" + LinkUrl + i + "\" class=\"p_num\">" + pNum + "</a>\n";
                    }
                    else
                    {
                        PageListText = PageListText + "<a class=\"p_curpage\">" + pNum + "</a>\n";
                    }
                }
            }
            this.PageList.Text = PageListText;
            //分页处理结束
        }
    }
}
